#include "callRPG.h"

// Callback exécuté lorsqu'une touche du clavier est relevée

void moveCallback(EsdlEvent* event) {

    int memoryX = 0, memoryY = 0;


    struct UserData* data = (UserData*) esdlGetUserData();

    data->dx = 0;
    data->dy = 0;
    memoryX = data->marker->x;
    memoryY = data->marker->y;

    // On regarde quelle touche a été pressé (flèches directionelles)
    switch (esdlGetKey(event)) {
        case KEY_UP:
            data->dy = -1;
            break;
        case KEY_DOWN:
            data->dy = 1;
            break;
        case KEY_LEFT:
            data->dx = -1;
            break;
        case KEY_RIGHT:
            data->dx = 1;
            break;
        default:
            break;
    }


    // Déplacement du sprite
    EsdlSprite* sprite = esdlGetSpriteEvent(event);
    memoryX = data->marker->x;
    memoryY = data->marker->y;
    if (detectionColision() != 1) {
        data->marker->x = sprite->x + data->dx*AMPLITUDE;
        data->marker->y = sprite->y + data->dy*AMPLITUDE;

    } else {
        data->marker->x = memoryX;
        data->marker->y = memoryY;
    }
    esdlSetSpritePosition(data->marker, data->marker->x, data->marker->y);
}

// Callback exécuté lorsque l'image gagne le focus

void markerFocusCallback(EsdlEvent* event) {
    printf("\nExecution du callback onFocus pour l'image marker.png\n");
    printf("L'image a gagne le focus.\n");
    printf("Vous pouvez maintenant utiliser les fleches directionelles pour deplacer le marqueur\n");
}

// Callback exécuté lorsque l'image perd le focus

void markerBlurCallback(EsdlEvent* event) {
    printf("\nExecution du callback onBlur pour l'image marker.png\n");
    printf("L'image a perdu le focus.\n");
}

void renderCallback() {
    struct UserData * data = (UserData*) esdlGetUserData();

    data->memoryX = data->marker->x;
    data->memoryY = data->marker->y;

    if (detectionColision() != 1) {
        data->marker->x = data->marker->x + data->dx*AMPLITUDE;
        data->marker->y = data->marker->y + data->dy*AMPLITUDE;

    } else {
        data->memoryX = data->marker->x;
        data->memoryY = data->marker->y;
    }

    esdlSetSpritePosition(data->marker, data->marker->x, data->marker->y);


}

void moveCallbackDown(EsdlEvent* event) {
    struct UserData* data = (UserData*) esdlGetUserData();
    //int amplitude = 10; // Nombre de pixels par déplacement

    // On regarde quelle touche a été pressé (flèches directionelles)


    switch (esdlGetKey(event)) {
        case KEY_UP:
            data->dy = -1;
            break;
        case KEY_DOWN:
            data->dy = +1;
            break;
        case KEY_LEFT:
            data->dx = -1;
            break;
        case KEY_RIGHT:
            data->dx = +1;
            break;
        default:
            break;
    }
}

void moveCallbackUp(EsdlEvent* event) {
    struct UserData * data = (UserData*) esdlGetUserData();
    data->dx = 0;
    data->dy = 0;
}

int detectionColision() {
    struct UserData * data = (UserData*) esdlGetUserData();

    int i, j;
    int r2d2H = 0, r2d2Px = 0, r2d2Py, r2d2W;
    int touchMur = 0;
    int result = 0;

    r2d2H = data->marker->surface->h;
    r2d2W = data->marker->surface->w;

    // vérifie le TOP
    if (data->dy == -1) {

        r2d2Px = data->marker->x;
        r2d2Py = abs(data->marker->y * data->dy);
        r2d2Py = r2d2Py - data->wallSprite[0][0]->surface->h;
        for (i = r2d2Px - AMPLITUDE; i < (r2d2Px + r2d2W); i++) {
            if (data->wallSprite[r2d2Py][i] != NULL) {
                touchMur++;
            }
        }
        if (touchMur > 0) {
            result = 1;
        }
    }

    // vérifie le BAS
    if (data->dy == +1) {

        r2d2Px = data->marker->x;
        r2d2Py = abs(data->marker->y * data->dy) + r2d2H;
        for (j = r2d2Py; j < r2d2Py + AMPLITUDE; j++) {

            for (i = r2d2Px - AMPLITUDE; i < r2d2Px + r2d2W; i++) {
                if (data->wallSprite[j][i] != NULL) {
                    touchMur++;
                }
            }

        }
        if (touchMur > 0) {
            result = 1;
        }
    }

    // vérifie le Left
    if (data->dx == -1) {

        r2d2Px = abs(data->marker->x * data->dx);
        r2d2Py = data->marker->y;
        r2d2Px = r2d2Px - data->wallSprite[0][0]->surface->w;
        for (j = r2d2Py - AMPLITUDE; j < r2d2Py + r2d2H; j++) {
            if (data->wallSprite[j][r2d2Px] != NULL) {
                touchMur++;
            } //Problème
        }
        if (touchMur > 0) {
            result = 1;
        }
    }

    // vérifie le Right

    if (data->dx == +1) {

        r2d2Px = abs(data->marker->x * data->dx) + r2d2W;
        r2d2Py = data->marker->y;
        for (i = r2d2Px; i < r2d2Px + AMPLITUDE; i++) {

            for (j = r2d2Py - AMPLITUDE; j < (r2d2Py + r2d2H); j++) {
                if (data->wallSprite[j][i] != NULL) {
                    touchMur++;
                }
            }

        }
        if (touchMur > 0) {
            result = 1;
        }
    }

    return result;

}
/*
int detectionPorte()
{
    
}*/